from PIL import Image, ImageDraw, ImageFont

def name_dot_with_spacing(name, 
                         dot_char="●", 
                         bg_char=" ", 
                         font_size=20,  # 小尺寸保持清晰
                         char_spacing=10):  # 字间距（像素）

    # 计算图片宽度：每个字占font_size宽度 + 字间距 + 边距
    # 字间距总长度 = (字数-1) * char_spacing
    total_spacing = (len(name) - 1) * char_spacing
    img_width = len(name) * font_size + total_spacing + 10  # 左右边距各5
    img_height = font_size + 10  # 上下边距各5
    img = Image.new("RGBA", (img_width, img_height), (255,255,255,0))

    # 加载清晰字体（微软雅黑优先）
    try:
        font = ImageFont.truetype("C:/Windows/Fonts/msyh.ttc", font_size)
    except:
        try:
            font = ImageFont.truetype("C:/Windows/Fonts/simhei.ttf", font_size)
        except:
            font = ImageFont.truetype("Arial Unicode MS", font_size)

    draw = ImageDraw.Draw(img)
    current_x = 5  # 起始x坐标（左边缘偏移5）
    for char in name:
        # 逐个绘制字符，每个字符后增加字间距
        draw.text((current_x, 5), char, font=font, fill=(0,0,0,255))
        current_x += font_size + char_spacing  # 移动到下一个字的位置

    # 生成点状图
    dot_str = ""
    for y in range(img_height):
        line = ""
        for x in range(img_width):
            _, _, _, a = img.getpixel((x, y))
            line += dot_char if a > 10 else bg_char
        dot_str += line + "\n"
    return dot_str

# 生成“刘海钩”（带字间距）
name_dot = name_dot_with_spacing("刘海钩", char_spacing=15)  # 字间距15像素
print(name_dot)